package com.hongyuan.sellfitness.ui.main.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.home.beans.MemberOsBean;
import com.hongyuan.sellfitness.ui.main.activity.MainActivity;
import com.hongyuan.sellfitness.ui.main.activity.RetrievePasswordActivity;
import com.hongyuan.sellfitness.ui.main.activity.WebActivity;
import com.hongyuan.sellfitness.ui.main.beans.MemberLoginBean;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class LoginPassWordViewModel extends CustomViewModel<MyRepository> {

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //是否同意协议
    public ObservableField<Boolean> isAgree = new ObservableField<>(false);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
        //同意协议
        public SingleLiveEvent<Boolean> agree = new SingleLiveEvent<>();
        //显示阅读协议弹框
        public SingleLiveEvent<Boolean> yueduxieyi = new SingleLiveEvent<>();
    }

    public LoginPassWordViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });

    //返回验证码登录
    public BindingCommand goCodeLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });
    //设置新密码
    public BindingCommand goGetNewPassword = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(RetrievePasswordActivity.class);
        }
    });
    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(isAgree.get()){
                login();
            }else{
                uc.yueduxieyi.call();
            }
        }
    });

    //服务协议
    public BindingCommand fwxy = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","https://fit.1667799.com/html/xy.html");
            bundle.putString("title","用户协议");
            startActivity(WebActivity.class,bundle);
        }
    });
    //服务协议
    public BindingCommand yszc = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","https://fit.1667799.com/html/ys.html");
            bundle.putString("title","隐私政策");
            startActivity(WebActivity.class,bundle);
        }
    });

    //同意协议
    public BindingCommand agreeXY = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.agree.setValue(isAgree.get());
        }
    });


    /*
    * 登录请求
    * */
    private void login(){
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        clearParams().setParams("m_mobile", userName.get()).setParams("m_pwd",password.get());

        //登录请求
        requestData(Constants.MEMBERLOGIN);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.MEMBERLOGIN){
            MemberLoginBean bean = (MemberLoginBean) dataBean;

            userToken.setM_id(bean.getM_id());
            userToken.setM_mobile(bean.getM_mobile());
            userToken.setRole_id(String.valueOf(bean.getRole_id()));

            //保存用户登录信息
            model.saveUser(bean);

            //极光推送设置别名或标签
            JPushInterface.setAlias(getApplication().getApplicationContext(), 100, userToken.getM_mobile());

            //获取支持的门店列表
            requestData(Constants.GET_MEMEBER_OS);
        }


        if(code == Constants.GET_MEMEBER_OS){
            List<MemberOsBean.ListBean> list = ((MemberOsBean)dataBean).getList();
            if(list != null && list.size() > 0){
                startActivity(MainActivity.class);
                finish();
            }else{
                ToastUtils.showShort("非会员用户，不予使用！");
                model.saveUser(null);
                userToken.setM_id(null);
            }
        }
    }
}
